<template>
  <div class="roles">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input v-model="iptval" placeholder="请输入内容...">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="goSearch(iptval)"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表单区域 -->
      <el-table :data="ordersList" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="是否支付">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status == 1" type="success"
              >已支付</el-tag
            >
            <el-tag v-else-if="scope.row.pay_status == 0" type="danger"
              >未支付</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-edit"
              @click="editOrdersVisible = true"
              plain
              circle
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-place"
              @click="logsOrders"
              plain
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editOrdersVisible"
      @close="closeEditOrders('editOrdersRef')"
    >
      <el-form :model="editOrdersInfo" ref="editOrdersRef">
        <el-form-item
          label="省市区/县"
          prop="city"
          label-width="100px"
          :rules="[{ required: true, message: '省市区/县不能为空' }]"
        >
          <el-cascader
            v-model="editOrdersInfo.city"
            :options="cityData"
            :props="{ expandTrigger: 'hover', ...propsQuery }"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          label="详细地址"
          label-width="100px"
          prop="address"
          :rules="[{ required: true, message: '详细地址不能为空' }]"
        >
          <el-input
            v-model="editOrdersInfo.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editOrdersVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrdersVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 物流详情对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="logOrdersVisible">
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(item, index) in logsList"
          :key="index"
          :timestamp="item.time"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
const dayjs = require("dayjs");
import cityData from "../assets/js/city.js";

export default {
  data() {
    return {
      // 获取订单数据列表
      ordersList: [],
      // 物流数据列表
      logsList:[],
      // 获取订单数据列表 请求参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
        user_id: "",
        pay_status: "",
        is_send: "",
        order_fapiao_title: "",
        order_fapiao_company: "",
        order_fapiao_content: "",
        consignee_addr: "",
      },
      // 订单列表数据 总数
      total: 0,
      // 搜索框值
      iptval: "",
      editOrdersVisible: false,
      logOrdersVisible: false,
      editOrdersInfo: {
        city: [],
        address: "",
      },
      // 城市信息
      cityData,
      propsQuery: {
        label: "label",
        value: "value",
        children: "children",
      },
    };
  },
  created() {
    this.getOrdersList();
  },
  methods: {
    // 获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo,
      });
      // console.log(res);
      if (res.meta.status !== 200)
        return this.$message.erro("订单列表获取失败");
      res.data.goods.forEach((item) => {
        // 处理订单价格，下单时间
        item.order_price = item.order_price.toFixed(2);
        item.create_time = dayjs(item.create_time).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      });
      this.ordersList = res.data.goods;
      this.total = res.data.total;
    },
    goSearch(val) {
      this.queryInfo.query = val;
      this.getOrdersList();
    },
    async logsOrders() {
      const {data:res} = await this.$http.get('kuaidi/1106975712662')
       console.log(res);
       if(res.meta.status !== 200) return this.$message.error('物流信息获取失败')
      //  if(res.data.length) return
       this.logsList = res.data;

      this.logOrdersVisible = true;
    },
    // 编辑订单对话框 关闭
    closeEditOrders(refname) {
      this.$refs[refname].resetFields();
    },
    // 分页 改变展示条数
    handleSizeChange(size) {
      this.queryInfo.pagesize = size;
      this.getOrdersList();
    },
    // 分页 改变当前页
    handleCurrentChange(cur) {
      this.queryInfo.pagenum = cur;
      this.getOrdersList();
    },
  },
};
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}
.el-cascader {
  width: 100%;
}
</style>